<?php
namespace Operationtest\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;

class MarchestoreTable extends AbstractTableGateway
{
    protected $table = 'MarcheStore';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Marchestore());
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    
    public function getMarcheStore($idMarche)
    {
        $idMarche  = (int) $idMarche;
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('idStore'));
        $select->from($this->table);
        
        $where = new Where();
        $where->equalTo('idMarche', $idMarche);
        $select->where($where);
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function getStoreMarche()
    {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('idMarche','idStore'));
        $select->from($this->table);
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        return $result;
    }
    
    public function attribMarcheStore($idMarcheStore, $idMarche, $idStore)
    {
        $data = array(
            'idMarcheStore' => $idMarcheStore,
            'idMarche' => $idMarche,
            'idStore' => $idStore,
        );
        
        $this->insert($data);
    }
    
    public function deleteMarcheStore($idMarche, $idStore)
    {    
        $this->delete(array('idMarche' => $idMarche, 'idStore' => $idStore));
    }
}
